package org.yi.fc.controller.admin.friendlink;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yi.fc.controller.base.BaseController;
import org.yi.fc.entity.FFriendLink;
import org.yi.fc.model.Pager;

import com.jfinal.plugin.activerecord.Page;

@RequiresAuthentication
public class FriendLinkController extends BaseController {
	
	private static final Logger logger = LoggerFactory.getLogger(FriendLinkController.class);
	
	@RequiresAuthentication
	@RequiresRoles(logical=Logical.OR,value={"admin","user"})
//	@RequiresPermissions(value={"admin"})
	public void add() {
		logger.trace("add friend link by {} method ...", getHttpMethod());
		if(isGetMethod()){
			render("add.html");
		} else if(isPostMethod()) {
			doAdd();
		}
	}
	
	public void doAdd() {
		StringBuffer sb = new StringBuffer();
		if(StringUtils.isBlank(getPara("keyword"))) {
			sb.append("关键词不能为空!");
		}
		if(StringUtils.isBlank(getPara("url"))) {
			sb.append("链接不能为空!");
		}
		//判断是否出现错误
		if(sb.length() == 0) {
			new FFriendLink().save(getRequest());
			redirect("/admin/friendlink/index");
		} else {
			addError("error", sb.toString());
			render("add.html");
		}
	}
	
	public void edit() {
		logger.trace("edit friend link by {} method ...", getHttpMethod());
		if(isGetMethod()){
			Integer id = getParaToInt("id");
			if(id != null) {
				FFriendLink flink = new FFriendLink().findById(id);
				setAttr("f", flink);
				render("add.html");
			} else {
				addError("要修改的链接id不能为空");
				forwardAction("/admin/friendlink/index");
			}
		} else if(isPostMethod()) {
			doEdit();
		}
	}
	
	public void doEdit() {
		StringBuffer sb = new StringBuffer();
		if(StringUtils.isBlank(getPara("keyword"))) {
			sb.append("关键词不能为空!");
		}
		if(StringUtils.isBlank(getPara("url"))) {
			sb.append("链接不能为空!");
		}
		//判断是否出现错误
		if(sb.length() == 0) {
			new FFriendLink().update(getRequest());
			redirect("/admin/friendlink/index");
		} else {
			addError("error", sb.toString());
			render("add.html");
		}
	}
	
	public void index() {
		logger.trace("friend link ...");
		
		Pager pager = getPager();
		Page<FFriendLink> page = FFriendLink.dao.getPager(pager);
		
		setAttr("page", page);
		render("list.html");
	}
	
	public void delete() {
		logger.trace("delete friend link ...");
		
		String id = getPara("id");
		if(StringUtils.isNotBlank(id)) {
			for(String i : id.split(",")) {
				logger.debug("delete friendlink, id: {}",  i);
				if(StringUtils.isNotBlank(i)) {
					new FFriendLink().delete(i);
				}
			}
		}
		redirect( "/admin/friendlink/index");
	}
	
	public void ban() {
		logger.trace("ban/repub friend link ...");
		
		String id = getPara("id");
		Integer action = getParaToInt("action");
		if(StringUtils.isNotBlank(id)) {
			for(String i : id.split(",")) {
				logger.debug("ban/repub friendlink, id: {}",  i);
				if(StringUtils.isNotBlank(i)) {
					if(action != null) {
						new FFriendLink().ban(action, i);
					} else {
						new FFriendLink().ban(i);
					}
				}
			}
		}
		redirect( "/admin/friendlink/index");
	}

}
